package com.whately.core.base.verifycode.kaptcha;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.whately.core.base.exception.ServiceException;
import com.whately.core.base.verifycode.bo.VerifyImgModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Random;

@Component
@Slf4j
public class CaptchaComp {
    @Autowired
    private DefaultKaptcha producer;

    /*public Map<String, Object> getCaptcha(HttpServletResponse response) throws ServletException, IOException {

        // 生成文字验证码
        String text = producer.createText();
        // 生成图片验证码
        ByteArrayOutputStream outputStream = null;
        BufferedImage image = producer.createImage(text);

        outputStream = new ByteArrayOutputStream();
        ImageIO.write(image, "jpg", outputStream);

        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();

        // 生成captcha的token
        Map<String, Object> map = Maps.newHashMap();
        map.put("img", encoder.encode(outputStream.toByteArray()));
        return map;
    }*/


    public VerifyImgModel getCaptcha() {
        VerifyImgModel model = generateText();

        byte[] imgBytes = getCaptchaImg(model.getCaptchaText());

        // 将图片转换成Base64编码
        model.setImg(Base64.encodeBase64String(imgBytes));
        return model;
    }

    private VerifyImgModel generateText(){
        VerifyImgModel model = new VerifyImgModel();

        String str="0123456789";
        StringBuilder sb=new StringBuilder(4);
        for(int i=0;i<4;i++)
        {
            char ch=str.charAt(new Random().nextInt(str.length()));
            sb.append(ch);
        }
        String text = sb.toString();
        model.setCaptchaText(text);
        model.setCaptchaValue(text);
        return model;
    }

    private VerifyImgModel generateText_calc(){
        Random random = new Random();
        int first = random.nextInt(10);
        int second = random.nextInt(90);
        int value = first + second;
        String text = new StringBuffer().append(first).append(" + ").append(second).append(" = ").toString();
        VerifyImgModel model = new VerifyImgModel();
        model.setCaptchaText(text);
        model.setCaptchaValue(String.valueOf(value));
        return model;
    }

    private byte[] getCaptchaImg(String text) {
        // 生成图片验证码
        BufferedImage image = producer.createImage(text);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try{
            ImageIO.write(image, "jpg", outputStream);
        }catch (IOException ioe){
            log.error("getCaptchaImg error ",ioe);
            throw new ServiceException(400,"获取验证码异常");
        }

        return outputStream.toByteArray();
    }

}
